//
//  LMFeedBackTextView.m
//  MeetCard
//
//  Created by longmin on 16/12/7.
//  Copyright © 2016年 timtian. All rights reserved.
//

#import "LMFeedBackTextView.h"
#import "UILabel+Size.h"

@interface LMFeedBackTextView ()<UITextViewDelegate>

@property(nonatomic, strong) UIView *grayView;

@property(nonatomic, strong) UITextView *textView;

@property(nonatomic, strong) UILabel *placeHolderLabel;

@end

@implementation LMFeedBackTextView

#pragma mark - 生命周期
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.grayView];
        [self.grayView addSubview:self.textView];
        [self.grayView addSubview:self.placeHolderLabel];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:UITextViewTextDidChangeNotification object:nil];
        
        self.backgroundColor = [UIColor whiteColor];
        self.textView.scrollsToTop = NO;
    }
    return self;
}
#pragma mark - Event

-(void)textDidChange {
    
    self.placeHolderLabel.hidden = self.textView.text.length;
    if (self.textDidChangeBlock) {
        self.textDidChangeBlock(self.text);
    }
    
}

#pragma mark - Public

+ (instancetype)feedbackTextViewWithPlaceHolder:(NSString *)placeHolder {
    
    LMFeedBackTextView *feedBackTextView = [[LMFeedBackTextView alloc] init];
    
    feedBackTextView.backgroundColor = [UIColor whiteColor];
    feedBackTextView.width = __kScreenWidth;
    feedBackTextView.height = 137.5 * __kwRatio;
    feedBackTextView.placeHolderLabel.text = placeHolder;
    
    return feedBackTextView;
}

-(void)setPlaceHolder:(NSString *)placeHolder {
    self.placeHolderLabel.text = placeHolder;
}

-(NSString *)placeHolder {
    return self.placeHolderLabel.text;
}


-(void)setText:(NSString *)text {
    self.textView.text = text;
}

-(NSString *)text {
    return self.textView.text;
}
#pragma mark - Private
#pragma mark - 数据源和代理方法
#pragma mark UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if (!self.maxLength) {
        return YES;
    }else {
        
        NSInteger sumLength = textView.text.length + text.length;
        
        if (sumLength <= self.maxLength) {
            return YES;
        }else {
            NSString *noticeStr = [NSString stringWithFormat:@"不能超过%zd个字",self.maxLength];
            [MBProgressHUD showText:noticeStr onView:nil];
            return NO;
        }
        
    }
    
}
#pragma mark - UIView -> 下面放继承自UIView的方法

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.grayView.width = self.width - MK_PADDING_30 * __kwRatio * 2;
    self.grayView.height = 97.5 * __kwRatio;
    self.grayView.top = MK_PADDING_50 * __kwRatio;
    self.grayView.centerX = self.width * 0.5;
    
    self.textView.width = self.grayView.width - MK_PADDING_30 * 2 * __kwRatio;
    self.textView.height = self.grayView.height - MK_PADDING_30 * __kwRatio + 7;
    self.textView.top = MK_PADDING_30 * __kwRatio - 7;
    self.textView.left = MK_PADDING_30 * __kwRatio;
    
    self.placeHolderLabel.size = [self.placeHolderLabel lm_sizeWithMargin:MK_PADDING_30 * 2 * __kwRatio];
    self.placeHolderLabel.top = self.placeHolderLabel.left = MK_PADDING_30 * __kwRatio;
    
}

#pragma mark - set方法和get方法

-(UITextView *)textView {
    if (_textView == nil) {
        _textView = [[UITextView alloc] init];
        _textView.delegate = self;
        _textView.font = _kFont32;
        _textView.textColor = kColor3C3C3C;
        _textView.backgroundColor = [UIColor clearColor];
    }
    return _textView;
}

-(UILabel *)placeHolderLabel {
    if (_placeHolderLabel == nil) {
        _placeHolderLabel = [[UILabel alloc] init];
        _placeHolderLabel.text = @"      祈望你心灵深处芳草永绿,青春常驻,笑口常开。祝你生日快乐,健康幸福!";
        _placeHolderLabel.textColor = kColorA6A6A6;
        _placeHolderLabel.numberOfLines = 0;
        _placeHolderLabel.font = _kFont32;
        _placeHolderLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _placeHolderLabel;
}

-(UIView *)grayView {
    if (_grayView == nil) {
        _grayView = [[UIView alloc] init];
        _grayView.backgroundColor = kColorD9D9D9;
    }
    return _grayView;
}

@end
